/*
 * Copyright (c) 2017.
 * @author fff
 * 17-10-23 下午5:07
 *
 */

package com.cpit.qualitycontrol.web.controller;

import com.cpit.platform.common.constant.LogConstant;
import com.cpit.platform.common.entity.Result;
import com.cpit.platform.common.entity.TableDataQueryCondition;
import com.cpit.platform.common.entity.TableJsonData;
import com.cpit.platform.dto.OperLog;
import com.cpit.platform.log.LogOperator;
import com.cpit.platform.service.OperLogService;
import com.cpit.qualitycontrol.common.constant.LogConstantEx;
import com.cpit.qualitycontrol.entity.CooperatorStaffer;
import com.cpit.qualitycontrol.service.CooperatorStafferService;
import com.cpit.qualitycontrol.service.impl.ServiceUtils;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 *
 * @author Hulunliang
 * @since 2017/11/4
 *
 */

@RestController
@RequestMapping(value = "/CooperatorStaffer")
public class CooperatorStafferController {
    private String logModule = "合作方人员：";
    private final String loggerName = this.getClass().getName();

    @Autowired
    private CooperatorStafferService cooperatorStafferService = null;
    @Autowired
    private OperLogService operLogService;

    @ApiOperation(value="查询合作方人员", notes="通过ID查询合作方人员详细信息")
    //@PreAuthorize("hasAuthority('queryCooperatorStaffer')")
    @RequestMapping(value="/queryCooperatorStaffer/{id}", method = RequestMethod.GET)
    public Result<CooperatorStaffer> queryCooperatorStaffer(@ApiParam(value = "合作方人员ID", required = true) @PathVariable(value = "id") Long id) {
        return new Result<>(Result.CODE_SUCCESS, "", this.cooperatorStafferService.findById(id));
    }

    @ApiOperation(value="查询所有合作方人员", notes="无条件查询合作方人员信息，返回对象List")
    //@PreAuthorize("hasAuthority('queryCooperatorStaffer')")
    @RequestMapping(value="/queryAllCooperatorStaffer", method = RequestMethod.GET)
    public List<CooperatorStaffer> queryAllCooperatorStaffer() {
        return this.cooperatorStafferService.findAll();
    }

    @ApiOperation(value="查询合作方人员列表", notes="通过条件查询合作方信息，返回List方式" +
            "支持模糊查询。")
    //@PreAuthorize("hasAuthority('queryCooperatorStaffer')")
    @RequestMapping(value="/queryCooperatorStaffer", method = RequestMethod.GET)
    public List<CooperatorStaffer> queryCooperatorStaffer(
            @ApiParam(value = "当前页数， 以1开始。") @RequestParam(value = "page", required = false, defaultValue = "1") int page,
            @ApiParam(value = "当前页的内容长度。") @RequestParam(value = "size", required = false, defaultValue = "20") int size,
            @ApiParam(value = "排序方式：asc为升序，desc为降序。") @RequestParam(value = "order", required = false, defaultValue = "asc") String order,
            @ApiParam(value = "排序字段，与dto中声明的字段名称一致。") @RequestParam(value = "orderBy", required = false, defaultValue = "id") String orderBy,
            @ApiParam(value = "姓名，模糊搜索") @RequestParam(value = "name", required = false, defaultValue = "") String name,
            @ApiParam(value = "公司名称，模糊搜索") @RequestParam(value = "companyName", required = false, defaultValue = "") String companyName) {
        return this.cooperatorStafferService.findByCondition(page, size, order, orderBy, name, companyName);
    }

    @ApiOperation(value="分页查询合作方人员", notes="通过条件查询合作方信息，返回jqGrid分页JSON形式，支持模糊查询。")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cond", value = "JqGrid的条件对象，由JqGrid根据js参数自动封装。", required = true, dataType = "TableDataQueryCondition")
    })
    @SuppressWarnings("rawtypes")
    //@PreAuthorize("hasAuthority('queryCooperatorStaffer')")
    @RequestMapping(value="/queryCooperatorStafferJqg", method = RequestMethod.GET)
    public TableJsonData queryCooperatorStafferJqg(TableDataQueryCondition cond) {
        return this.cooperatorStafferService.findByConditionJqg(cond);
    }

    @ApiOperation(value="创建合作方人员", notes="用户手工指定合作方人员并创建")
    @ApiImplicitParam(name = "data", value = "合作方人员对象", required = true, dataType = "CooperatorStaffer")
    //@PreAuthorize("hasAuthority('createCooperatorStaffer')")
    @RequestMapping(value="/createCooperatorStaffer", method = RequestMethod.POST)
    public Result<CooperatorStaffer> createCooperatorStaffer(@RequestBody CooperatorStaffer data) {
        try{
            CooperatorStaffer retCooperatorStaffer = this.cooperatorStafferService.create(data);
            writeLog(LogConstant.OPER_CREATE, Result.CODE_SUCCESS,logModule+retCooperatorStaffer.getName());
            return new Result<>(Result.CODE_SUCCESS, "", retCooperatorStaffer);
        } catch(Exception e) {
            LogOperator.err(loggerName, "createCooperatorStaffer error:" + data, e);
            writeLog(LogConstant.OPER_CREATE, Result.CODE_FAILED,logModule+e.getMessage());
            return new Result<>(Result.CODE_FAILED, e.getMessage(), data);
        }
    }

    @ApiOperation(value="修改合作方人员", notes="用户手工指定合作方人员并修改")
    @ApiImplicitParam(name = "data", value = "合作方人员对象", required = true, dataType = "CooperatorStaffer")
    //@PreAuthorize("hasAuthority('modifyCooperatorStaffer')")
    @RequestMapping(value="/modifyCooperatorStaffer", method = RequestMethod.POST)
    public Result<CooperatorStaffer> modifyCooperatorStaffer(@RequestBody CooperatorStaffer data) {
        try{
            CooperatorStaffer retCooperatorStaffer = this.cooperatorStafferService.modify(data);
            writeLog(LogConstant.OPER_MODIFY, Result.CODE_SUCCESS,logModule+data.getName());
            return new Result<>(Result.CODE_SUCCESS, "", retCooperatorStaffer);
        } catch(Exception e) {
            LogOperator.err(loggerName, "modifyCooperatorStaffer error:" + data, e);
            writeLog(LogConstant.OPER_MODIFY, Result.CODE_FAILED,logModule+e.getMessage());
            return new Result<>(Result.CODE_FAILED, e.getMessage(), data);
        }
    }

    @ApiOperation(value="删除合作方人员", notes="在系统中删除指定合作方人员")
    @ApiImplicitParam(name = "data", value = "合作方人员对象", required = true, dataType = "CooperatorStaffer")
    //@PreAuthorize("hasAuthority('deleteCooperatorStaffer')")
    @RequestMapping(value="/deleteCooperatorStaffer", method = RequestMethod.DELETE)
    public Result<CooperatorStaffer> deleteCooperatorStaffer(@RequestBody CooperatorStaffer data) {
        try{
            this.cooperatorStafferService.delete(data);
            writeLog(LogConstant.OPER_DELETE, Result.CODE_SUCCESS,logModule+data.getName());
            return new Result<>(Result.CODE_SUCCESS, "", data);
        } catch(Exception e) {
            LogOperator.err(loggerName, "deleteCooperatorStaffer error:" + data, e);
            writeLog(LogConstant.OPER_DELETE, Result.CODE_FAILED,logModule+e.getMessage());
            return new Result<>(Result.CODE_FAILED, e.getMessage(), data);
        }
    }

    @ApiOperation(value="删除合作方人员", notes="在系统中删除指定合作方人员")
    //@PreAuthorize("hasAuthority('deleteCooperatorStaffer')")
    @RequestMapping(value="/deleteCooperatorStaffer/{id}", method = RequestMethod.DELETE)
    public Result<CooperatorStaffer> deleteCooperatorStaffer(@ApiParam(value = "合作方人员ID", required = true) @PathVariable("id") Long id) {
        try{
            CooperatorStaffer cooperatorStaffer = cooperatorStafferService.findById(id);
            this.cooperatorStafferService.delete(id);
            writeLog(LogConstant.OPER_DELETE, Result.CODE_SUCCESS,logModule+cooperatorStaffer.getName());
            return new Result<>(Result.CODE_SUCCESS, id.toString());
        } catch(Exception e) {
            LogOperator.err(loggerName, "deleteCooperatorStaffer error:id=" + id, e);
            writeLog(LogConstant.OPER_DELETE, Result.CODE_FAILED,logModule+e.getMessage());
            return new Result<>(Result.CODE_FAILED, e.getMessage());
        }
    }

    private  void  writeLog(String op,String result, String detail){
        OperLog operLog =new OperLog();
        operLog.setOperateTime(new Date());
        operLog.setModule(LogConstantEx.MODULE_COOPERATOR);
        operLog.setCommand(op);
        operLog.setResult(result);
        operLog.setOperateUser(ServiceUtils.getLoginUser().getUsername());
        operLog.setDetail(detail);
        operLogService.writeLog(operLog);
    }
    
}
